package com.vincent.service;


import com.vincent.common.enums.ResultMapKey;
import com.vincent.dao.MenuDao;
import com.vincent.dao.MenuPermissionDao;
import com.vincent.dao.PermissionDao;
import com.vincent.model.AdminMenu;
import com.vincent.model.MenuPermission;
import com.vincent.model.Permission;
import com.vincent.vo.MenuSaveVO;
import com.vincent.vo.MenuUpdateVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Vincent on 2014/12/30.
 */
@Service("menuService")
public class MenuService {
    @Resource
    private MenuDao menuDao;
    @Resource
    private MenuPermissionDao menuPermissionDao;
    @Resource
    private PermissionDao permissionDao;


    public Map<String,Object> findMenus(){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        //获取菜单
        List<AdminMenu> menuAllList = menuDao.selectAll();
        //获取所有权限
        List<Permission> permissions = permissionDao.selectAll();
        //获取所有菜单权限
        List<MenuPermission> menuPermissions = menuPermissionDao.selectAll();

        //获取权限处理后的菜单
        List<AdminMenu> menuList = selectByPermission(menuAllList, permissions, menuPermissions);

        List<AdminMenu> menuByParentId = getMenuByParentId(null, menuList);

        //数据对象
        Map<String,Object> dataMap=new HashMap<String, Object>();
        dataMap.put("rows", menuByParentId);
        dataMap.put("total", menuByParentId.size());
        resultMap.put(ResultMapKey.SUCCESS.getCode(),dataMap);
        return resultMap;
    }
    public Map<String,Object> saveMenu(MenuSaveVO vo){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        AdminMenu menu = new AdminMenu();
        menu.setIcon(vo.getIcon());
        menu.setParentid(vo.getParentId());
        menu.setName(vo.getName());
        menu.setUrl(vo.getUrl());
        menuDao.saveMenu(menu);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"保存菜单成功");
        return resultMap;
    }

    public Map<String,Object> updateMenu(MenuUpdateVO vo){
        Map<String,Object> resultMap=new HashMap<String, Object>();

        AdminMenu menu = menuDao.getById(vo.getMenuId());
        if(menu==null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"菜单不存在");
            return resultMap;
        }

        menu = new AdminMenu();
        menu.setIcon(vo.getIcon());
        menu.setName(vo.getName());
        menu.setId(vo.getMenuId());
        menu.setUrl(vo.getUrl());
        menuDao.udpateMenu(menu);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"编辑菜单成功");
        return resultMap;
    }

    public Map<String,Object> getMenu(Integer id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        AdminMenu menu = menuDao.getById(id);
        if(menu==null){
            resultMap.put(ResultMapKey.ERROR.getCode(),"菜单不存在");
            return resultMap;
        }
        resultMap.put(ResultMapKey.SUCCESS.getCode(),menu);
        return resultMap;
    }

    public Map<String,Object> deleteMenus(Integer []id){
        Map<String,Object> resultMap=new HashMap<String, Object>();
        menuPermissionDao.deleteByMenuIds(id);
        menuDao.deleteMenus(id);
        resultMap.put(ResultMapKey.SUCCESS.getCode(),"删除菜单成功");
        return resultMap;
    }

    public static List<AdminMenu> getMenuByParentId(Integer parentId,List<AdminMenu> menus){
        List<AdminMenu> menuList=new ArrayList<AdminMenu>();
        for(AdminMenu menu:menus){
            Integer parentMenuId = menu.getParentid();
            if(parentId==null?null== parentMenuId :(parentMenuId!=null&&parentId.intValue()== parentMenuId.intValue())){
                List<AdminMenu> children = getMenuByParentId(menu.getId(), menus);
                menu.setChildren(children);
                menuList.add(menu);
            }
        }
        return menuList;
    }

    public static List<AdminMenu> selectByPermission(List<AdminMenu> menuAllList, List<Permission> permissionAllList, List<MenuPermission> menuPermissionList){
        List<AdminMenu> menuList=new ArrayList<AdminMenu>();
        for(AdminMenu menu:menuAllList){
            Integer menuId = menu.getId();
            List<Permission> menuPermissions = getMenuPermissions(menuId, permissionAllList, menuPermissionList);
            if(menuPermissions.isEmpty()){
                menuList.add(menu);
            }else{
                Subject subject = SecurityUtils.getSubject();
                String []perNames=new String[menuPermissions.size()];
                for(int i=0;i<menuPermissions.size();i++){
                    perNames[i]=menuPermissions.get(i).getName();
                }
                if(subject.isPermittedAll(perNames)){
                    menuList.add(menu);
                }
            }
        }
        return menuList;
    }
    public static List<Permission> getMenuPermissions(Integer menuId, List<Permission> permissionAllList, List<MenuPermission> menuPermissionList){
        List<Permission> permissions=new ArrayList<Permission>();
        for(MenuPermission menuPermission:menuPermissionList){
            if(menuPermission.getMenuId()==menuId){
                Integer permissionId = menuPermission.getPermissionId();
                Permission permissionById = PermissionService.getPermissionById(permissionId, permissionAllList);
                if(permissionById!=null){
                    permissions.add(permissionById);
                }
            }
        }
        return permissions;
    }
}
